package com.egene.tw.dao;

import com.easemob.EMCallBack;
import com.easemob.chat.EMChatManager;
import com.egene.tw.model.User;
import com.egene.tw.pref.UserPref_;

import org.androidannotations.annotations.EBean;
import org.androidannotations.annotations.sharedpreferences.Pref;

/**
 * Created by WZX on 2015/8/17.
 */
@EBean
public class UserDao {

    @Pref
    UserPref_ userPref;

    public void setUser(User user) {
        userPref.edit()
                .birthday().put(user.getBirthdayString())
                .loginId().put(user.getLoginID())
                .userId().put(user.getUserID())
                .userNo().put(user.getUserNo())
                .userName().put(user.getUserName())
                .weight().put(user.getWeight())
                .height().put(user.getHeight())
                .mobile().put(user.getMobile())
                .sex().put(user.getSex())
                .userType().put(user.getUserType())
                .apply();
    }

    public void  setPw(String pw){
        userPref.edit().password().put(pw).apply();
    }

    public User getUser() {
        User user = new User();
        user.setBirthdayString(userPref.birthday().get());
        user.setLoginID(userPref.loginId().get());
        user.setPassword(userPref.password().get());
        user.setUserID(userPref.userId().get());
        user.setUserNo(userPref.userNo().get());
        user.setUserName(userPref.userName().get());
        user.setHeight(userPref.height().get());
        user.setWeight(userPref.weight().get());
        user.setMobile(userPref.mobile().get());
        user.setSex(userPref.sex().get());
        user.setUserType(userPref.userType().get());
        return user;
    }

    public void removeUser(){
        userPref.clear();
        //此方法为异步方法
        EMChatManager.getInstance().logout(new EMCallBack() {

            @Override
            public void onSuccess() {

            }

            @Override
            public void onProgress(int progress, String status) {
                // TODO Auto-generated method stub

            }

            @Override
            public void onError(int code, String message) {
                // TODO Auto-generated method stub

            }
        });
    }

}
